package com.example.miaopin.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.example.miaopin.R;
import com.example.miaopin.utils.ImageUtil;
import com.example.miaopin.utils.SPUtil;
import com.example.miaopin.view.RoundView;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Administrator 园fragment
 */
public final class YaunFragment extends BaseFragment implements OnClickListener {

	private boolean isbegin = false;// 是否开始监听
	public List<String> datas = new ArrayList<String>();// 提示语
	private ListView listView;
	private RoundView roundView;
	private MyAdapter adapter;
	private int num = 0;

	@Override
	protected View initview() {
		view = inflater2(R.layout.yuanlayout);
		roundView = (RoundView) view.findViewById(R.id.yuan);
		roundView.setFoot(0, 0);
		roundView.setOnClickListener(this);
		listView = (ListView) view.findViewById(R.id.tongzhi);
		adapter = new MyAdapter();
		listView.setAdapter(adapter);

		return view;
	}

	/**
	 * 改变提示语
	 */
	private void changeDatas() {

		final String[] stringArray = getResources().getStringArray(R.array.erroyrun);

		final Handler handler = new Handler();
		Runnable runnable = new Runnable() {
			public void run() {
				if (datas.size() >= 5) {
					handler.removeCallbacks(this);
				} else {
					datas.add(stringArray[num++]);
					handler.postDelayed(this, 10000);
					adapter.notifyDataSetChanged();
				}
			}
		};
		handler.post(runnable);
	}

	@SuppressLint("ResourceAsColor")
	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return datas.size();
		}

		@Override
		public Object getItem(int position) {
			return datas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView textView = new TextView(mContext);
			textView.setText(datas.get(position));
			textView.setTextColor(R.color.tomato);
			textView.setTextSize(17);
			return textView;
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.yuan:
			if (new SPUtil(mContext).getBoolean("isLogin", false)) {
				roundView.setText(isbegin ? "开始秒赞" : "正在运行");
				roundView.setFoot(!isbegin ? .5f : 0, !isbegin ? -.5f : 0);
				isbegin = !isbegin;
				if (isbegin) {
					changeDatas();
				}
			} else {
				Toast.makeText(mContext, "请点击头像登录", 1).show();
			}

			break;
		}

	}

	@Override
	protected void setTtitle(String string) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setImage(Bitmap bitmap) {
		// TODO Auto-generated method stub

	}

}
